#include "MonitoringDeviceDelete.h"
#include "ui_MonitoringDeviceDelete.h"

MonitoringDeviceDelete::MonitoringDeviceDelete(QWidget *parent,userModel*model) :
    QWidget(parent),
    ui(new Ui::MonitoringDeviceDelete),
    umodel(model),
    devmodel(this),
    delegate(this)
{
    ui->setupUi(this);

    //devmodel.setDeviceList(deviceList);
    ui->tableView->setModel(&devmodel);
    ui->tableView->setItemDelegate(&delegate);


    connect(SnaillTcpClient::instance(), &SnaillTcpClient::dataReceived, this, &MonitoringDeviceDelete::handleDataReceived);


    connect(&devmodel, &MonitoringDeviceTableModel::deviceDelete, this, &MonitoringDeviceDelete::onDeviceDeleted);
    connect(&delegate, &DeviceDeleteBtnDelegate::editButtonClicked, this, &MonitoringDeviceDelete::onEditButtonClicked);
    connect(&delegate, &DeviceDeleteBtnDelegate::checkDevice, this, &MonitoringDeviceDelete::checkDevice);
    connect(&devmodel, &MonitoringDeviceTableModel::dataChanged, this, &MonitoringDeviceDelete::onDataChanged);
}
void MonitoringDeviceDelete::handleDataReceived(const QByteArray &data)
{
    qDebug()<<"==+++=="<<data;
    QJsonDocument jsonDoc = QJsonDocument::fromJson(data);
    QJsonObject jsonObj = jsonDoc.object();
    QString action = jsonObj["action"].toString();
    if(action=="deviceList"){
        qDebug()<<"deviceList==="<<jsonObj;

        deviceList =getDeviceList(data);
        devmodel.updateAllDeviceList(deviceList);
    }else if (action=="deviceDelete") {

    }
}

QList<deviceModel> MonitoringDeviceDelete::getDeviceList(const QString &jsonString)
{
    QList<deviceModel> deviceList;
    QJsonDocument jsonDoc = QJsonDocument::fromJson(jsonString.toUtf8());
    if (jsonDoc.isObject()) {
        QJsonObject rootObject = jsonDoc.object();
        if (rootObject.contains("data") && rootObject["data"].isArray()) {
            QJsonArray jsonArray = rootObject["data"].toArray();
            for (const QJsonValue &value : jsonArray) {
                if (value.isObject()) {
                    QJsonObject deviceObject = value.toObject();
                    deviceModel device;
                    device.id = deviceObject["id"].toString();
                    device.user_id = deviceObject["user_id"].toString();
                    device.name = deviceObject["name"].toString();
                    device.type = deviceObject["type"].toString();
                    device.location = deviceObject["location"].toString();
                    device.manufacturer = deviceObject["manufacturer"].toString();
                    device.model = deviceObject["model"].toString();
                    device.status = deviceObject["status"].toString();
                    device.installation_date = deviceObject["installation_date"].toString();
                    deviceList.append(device);
                }
            }
        }
    }
    return deviceList;
}

void MonitoringDeviceDelete::onDeviceDeleted(const deviceModel &device)
{
    QJsonObject jsonObj;
    jsonObj["action"] ="deviceDelete";
    jsonObj["id"] =device.id;
    if (SnaillTcpClient::instance()->sendJsonData(jsonObj)) {
        //  qDebug() << "deviceList data sent successfully";
    } else {

        return ;
    }
    QString na1="删除了";
     LOG_INFO(na1+device.name+"设备");
}

void MonitoringDeviceDelete::onEditButtonClicked(int row)
{
    devmodel.setEditRow(row);
}

void MonitoringDeviceDelete::showEvent(QShowEvent *event)
{
    qDebug() << "页面显示，进入页面操作可在此处添加";
    QJsonObject jsonObj;
    jsonObj["action"] ="deviceList";
    jsonObj["user_id"] =QString::number(umodel->id);
    if (SnaillTcpClient::instance()->sendJsonData(jsonObj)) {
        //  qDebug() << "deviceList data sent successfully";
    } else {

        return ;
    }
    QWidget::showEvent(event);
}
void MonitoringDeviceDelete::onDataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight, const QVector<int> &)
{
    qDebug() << "数据已更新，起始行: " << topLeft.row() << " 起始列: " << topLeft.column() << " 结束行: " << bottomRight.row() << " 结束列: " << bottomRight.column();

    // 在这里可以添加具体的数据更新处理逻辑
    // 例如，可以遍历更新的数据范围，获取新的数据并进行相应的操作
    for (int row = topLeft.row(); row <= bottomRight.row(); ++row) {
        for (int col = topLeft.column(); col <= bottomRight.column(); ++col) {
            QModelIndex index = devmodel.index(row, col);
            QVariant data = devmodel.data(index);
            qDebug() << "更新后的数据，行: " << row << " 列: " << col << " 值: " << data.toString();
            deviceModel model=deviceList[row];
            QString str;
            switch (col) {
            case 2: str = model.name; break;
            case 3: str = model.type; break;
            case 4: str = model.location; break;
            case 5: str = model.manufacturer; break;
            case 6: str= model.model; break;
            case 7: str= model.status; break;
            default: break;
            }

            if(str!=data.toString()){
                qDebug()<<"更新后的数据是";

                QJsonObject jsonObj;
                jsonObj["action"] ="deviceUpdate";
                jsonObj["user_id"] =model.user_id;
                jsonObj["id"] =model.id;
                jsonObj["str"] =data.toString();
                jsonObj["col"] =QString::number(col);
                LOG_INFO(model.user_id+"修改了"+model.name+"设备");
                if (SnaillTcpClient::instance()->sendJsonData(jsonObj)) {
                     qDebug() << "deviceList data sent successfully";

                } else {
                        qDebug() << "deviceList data sent error";
                    return ;
                }
            }

        }
    }
}

void MonitoringDeviceDelete::checkDevice(int row)
{
    deviceModel model=deviceList[row];
    QWidget*par=this->parentWidget();
    qDebug()<<"par==="<<par;
    p_mainData=new MonitoringMainData(nullptr,&model,umodel);
    //this->setEnabled(false);
    p_mainData->show();
}

MonitoringDeviceDelete::~MonitoringDeviceDelete()
{
    delete ui;
}
